package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import org.apache.bcel.generic.IF_ACMPEQ;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSeedValueMDP;

import java.util.List;

public class MaterialTypCcontrol extends InterruptListener implements InterruptListenerInterface {


    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {


        System.out.println("任务完成校验 = " + ctx);
        ProcessInstance processInstance = ctx.getProcessInstance();

        String bindId = processInstance.getId();
        List<BO> list1 = SDK.getBOAPI().query("BO_EU_SAP_JBST").bindId(bindId).list();
        String material ="";
        if (list1 !=null && list1.size()>0){
            material = list1.get(0).getString("MATERIAL");
            for (BO bo : list1) {
                String matlType = bo.get("MATL_TYPE") != null ? bo.getString("MATL_TYPE") : "";

                //物料类型校验
                if ("Z001".equals(matlType) || "Z002".equals(matlType)||"Z005".equals(matlType)){

                    String basicMatl = bo.get("BASIC_MATL") != null ? bo.getString("BASIC_MATL") : "";
                    String pageformat = bo.get("PAGEFORMAT") != null ? bo.getString("PAGEFORMAT") : "";

                    System.out.println("pageformat = " + pageformat);

                    if (pageformat ==null && "".equals(pageformat)){

                        throw new BPMNError("0332","基本视图第物料类型为{"+matlType+"}，格式页为必填！");
                    }

                    if (basicMatl ==null && "".equals(basicMatl)){
                        throw new BPMNError("0332","基本视图第物料类型为{"+matlType+"}，产品型号为必填！");
                    }
                }


                //3码卡控
                String pageformat = bo.get("PAGEFORMAT") != null ? bo.getString("PAGEFORMAT") : "";

                if (pageformat !=null  && !"".equals(pageformat)){

                    List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_CCST").addQuery("MAINID=", bo.getId()).list();

                    if (list !=null && list.size()>0){
                        for (BO bo1 : list) {
                            String shelfLife = bo1.get("SHELF_LIFE")!=null ? bo1.getString("SHELF_LIFE"):"";
                            if ("Z001".equals(matlType) || "Z002".equals(matlType)||"Z005".equals(matlType)){
                                if ("".equals(shelfLife)){
                                    throw new BPMNError("0332","总货架寿命不能为空");
                                }

                            }



                        }
                    }else{
                        throw new BPMNError("0332","基本视图页码有数据存储视图必须有数据");
                    }
                }
            }

        }









        List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_ZCHJCH").bindId(bindId).list();
        if (!material.matches("^(1-WF|1-IC-00).*")) {
            if (list ==null && list.size()==0){
                throw new BPMNError("0332","直材或间材必填");
            }
        }


        if (list!=null && list.size()>0){
            for (BO bo1 : list) {
            String zgg=    bo1.get("ZGG") !=null ? bo1.getString("ZGG"):"";
            if (!"".equals(zgg)){
                if (!zgg.contains("+")  && !zgg.contains("-") ){
                    throw new BPMNError("0332","元器规格必须包含+或者-");
                }

            }

            }
        }

       //sap码校验


        return true;
    }

    public static void main(String[] args) {
        String material="1-WF-00-0000001";
        if (material.matches("^(1-WF|1-IC-00).*")) {
            System.out.println("material = " + material);
        }else {
            System.out.println("material1 = " + material);
        }

    }
}
